package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.core.common.annotation.MenuDesc;
import com.core.common.gen.BaseServiceImpl;
import com.core.family.admin.domain.po.SysMenu;
import com.core.family.admin.mapper.SysMenuMapper;
import com.core.family.admin.service.SysMenuService;
import com.core.family.springUtils.ApplicationUtil;
import org.springframework.aop.support.AopUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    /***
     * 更新menu 菜单
     * @param list
     * @return
     */
    public boolean updateOrSaveBatch(List<SysMenu> list) {
        if (org.springframework.util.CollectionUtils.isEmpty(list)) {
            return false;
        }

        List<String> menuPaths = list.stream().map(SysMenu::getMenuPath).collect(Collectors.toList());

        LambdaQueryWrapper<SysMenu> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SysMenu::getMenuPath, menuPaths);
        //  .eq(SysMenu::getOrgId, UserUtil.getOrgId())
        List<SysMenu> dbList = Optional.ofNullable(this.baseMapper.selectList(queryWrapper)).orElse(new ArrayList<>());

        List<SysMenu> tempList = new ArrayList<>();
        for (SysMenu menu : list) {
            for (SysMenu dbMenu : dbList) {
                if (menu.getMenuPath().equals(dbMenu.getMenuPath())) {
                    menu.setId(dbMenu.getId());
                    tempList.add(menu);
                    break;
                }
            }
        }
        list.removeAll(tempList);
        if (!org.springframework.util.CollectionUtils.isEmpty(tempList)) {
            updateBatchById(tempList);
        }
        if (list.size() == 0) {
            return true;
        }

        return saveBatch(list);
    }


    /***
     * 根据MenuDesc 注解插入菜单相对路径到数据库
     * @param orgId3
     * @return
     */
    @Override
    public boolean initMenuUrl(Integer orgId) {
        Map map = ApplicationUtil.getApplicationContext().getBeansWithAnnotation(MenuDesc.class);
        List<SysMenu> mapList = new ArrayList<>();

        for (Object o : map.values()) {
            Class<?> aClass = AopUtils.getTargetClass(o);
            System.out.println("拿到Class:" + aClass);
            String s = aClass.getName();
            RequestMapping annotation = aClass.getAnnotation(RequestMapping.class);
            MenuDesc menuDesc = aClass.getAnnotation(MenuDesc.class);
            String desc = menuDesc.desc();
            String code = menuDesc.code();

            String[] urlPAth = annotation.value();
            String baseUrl = urlPAth[0];

            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                String[] urls = null;
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                MenuDesc menuDescM = method.getAnnotation(MenuDesc.class);
                if (null != getMapping) {
                    urls = getMapping.value();
                }
                if (null != postMapping) {
                    urls = postMapping.value();
                }
                if (null != requestMapping) {
                    urls = requestMapping.value();
                }
                if (null==urls){
                    continue;
                }

                String url = baseUrl + urls[0];
                SysMenu menu = convert2Menu(desc, code, url);
                //menu.setOrgId(orgId);
                if (null != menuDescM) {
                    menu.setMenuName(desc + menuDescM.desc());
                }
                mapList.add(menu);
            }

        }
        return updateOrSaveBatch(mapList);

    }

    private SysMenu convert2Menu(String desc, String code, String url) {

        SysMenu menu = new SysMenu();
        menu.setMenuPath(url);

        menu.setMenuName(desc);
        menu.setMenuCode(code + "_" + url);
        if (url.contains("getPage")) {
            menu.setMenuName(desc + "分页查询");
            menu.setMenuCode(code + "_getPage");
        }
        if (url.contains("getList")) {
            menu.setMenuName(desc + "批量查询");
            menu.setMenuCode(code + "_getList");
        }
        if (url.contains("update") || url.contains("edit")) {
            menu.setMenuName(desc + "编辑");
            menu.setMenuCode(code + "_update");
        }
        if (url.contains("insert") || url.contains("save")) {
            menu.setMenuName(desc + "新增");
            menu.setMenuCode(code + "_insert");
        }
        if (url.contains("batch/insert") || url.contains("batch/save")) {
            menu.setMenuName(desc + "批量新增");
            menu.setMenuCode(code + "_batch_insert");
        }
        if (url.contains("del") || url.contains("remove")) {
            menu.setMenuName(desc + "删除");
            menu.setMenuCode(code + "_del");
        }
        if (url.contains("batch/del") || url.contains("batch/remove")) {
            menu.setMenuName(desc + "批量删除");
            menu.setMenuCode(code + "_batch_del");
        }
        if (url.contains("get/")) {
            menu.setMenuName(desc + "查询");
            menu.setMenuCode(code + "_get");
        }

        return menu;

    }
}

